using System;
using System.Collections;
using System.Text;
using Sedna.Core.Controller.Configurations;

namespace Sedna.Core.Controller.Common
{
    public class GenericCommandArgumentGenerator: ICommandArgumentGenerator
    {

        private ICommandArgumentGenerator next = CommandConfiguration.EmptyStringCommandArgumentGenerator.Instance;
        private IList actionParams;

        public ICommandArgumentGenerator Next
        {
            set { next = value; }
        }

        public IList ActionParams
        {
            set { actionParams = value; }
        }


        public string GenerateCommandArguments(ICommandConfiguration configuration, IExecutionContext executionContext)
        {
            return BuildArguments(executionContext) + next.GenerateCommandArguments(configuration, executionContext);
        }

        private string BuildArguments(IExecutionContext executionContext)
        {
            if (actionParams == null || actionParams.Count == 0)
            {
                return string.Empty;
            }

            StringBuilder builder = new StringBuilder(actionParams.Count*60);
            
            foreach (IActionParam actionParam in actionParams)
            {
                object value = actionParam.GetValue(executionContext);
                
                builder.AppendFormat("{0}={1};", actionParam.Name, value!=null?value.ToString():null);
            }

            return builder.ToString();
        }
    }
}
